package im.yisme.view.ui.animation;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import im.yisme.view.ui.animation.pojo.LevelPOJO;

public class Level extends Thread{

	private LevelPOJO level;
	
	private static final int ANIMATION_UPDATE_INTERVAL = 5;
	private static final int ANIMATION_ONCE_TIMES = 30;
	private int count;
	
	private static final int X = 540;
	private static final int Y = 250;
	
	private Bitmap ori;
	
	public Level(LevelPOJO level){
		
		this.level = level;
		ori = level.getLevel();
		count = 0;
	}
	public void run(){
		
		while(count < ANIMATION_ONCE_TIMES){
			
			try {
				
				if(count<20){
					float scale = (float)(level.getLevel().getWidth()+count) / level.getLevel().getWidth();
					Bitmap tempLevel = zoomIn(ori, scale);
					level.setLevel(tempLevel);
					PointF pointF = new PointF(X-tempLevel.getWidth()/2,Y-tempLevel.getHeight()/2);
					level.setPoint(pointF);
				}
				else {
					level.setAlpha(level.getAlpha()-10);
				}
				
				
				count++;
				Thread.sleep(ANIMATION_UPDATE_INTERVAL);
			
			
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		level.setLevel(null);
	}
	
	private Bitmap zoomIn(Bitmap input,float scale){
		
		Matrix matrix = new Matrix();
	
        matrix.postScale(scale, scale);
            
        Bitmap output = Bitmap.createScaledBitmap(input, (int)(input.getWidth()*scale), (int)(input.getHeight()*scale), false);
        
        
        return output;
	}
}
